package com.adds.lvds.core.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;
@SuppressWarnings("all")
public class FileUtils {
	private static SimpleDateFormat sdf_y_M_d_H_m_s = new SimpleDateFormat("yyyyMMddHHmmss");
	
	/**
	 * 使用  MultipartFile file 转存文件
	 * @author Bobkate
	 * @throws IOException 
	 * @throws IllegalStateException 
	 * */
	public static String uploadFile(MultipartFile multipartFile, String filePath) throws IllegalStateException, IOException {
		//获取原文件名称
		String origName = multipartFile.getOriginalFilename();
		//获取文件类型
		String type = origName.indexOf(".")!=-1?origName.substring(origName.lastIndexOf(".")+1, origName.length()):null;
		//为防止文件重名  按规则生成名称
		String fileName = sdf_y_M_d_H_m_s.format(new Date()) + UUID.randomUUID().toString() + origName.substring(origName.lastIndexOf("."));
		
		//判断是否存在目录
        File targetFile=new File(filePath,fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();//创建目录
        }
        multipartFile.transferTo(targetFile);
        return fileName;
	}
}
